package niuke;

import java.util.Arrays;

/**
 * Created by asus on 2017/12/11.
            */
    public class DisdinctPermutation {
        private static int findCeil(char[] str,char first,int low,int high){
            int ceilIndex=low;
            for(int i=low+1;i<=high;++i){
                if(str[i]>first&&str[i]<str[ceilIndex])
                    ceilIndex=i;
            }
            return ceilIndex;
        }
        static int x=1;
        public static void sortedPermutation(char[] str){
            int size=str.length;
            Arrays.sort(str);

            boolean isFinish=false;
            while (!isFinish){
                System.out.println((x++)+"  "+Arrays.toString(str));

                int i;
                for(i=size-2;i>=0;i--){
                    if(str[i]<str[i+1])
                        break;
                }
                if(i==-1)
                    isFinish=true;
                else{
                    int ceilIndex=findCeil(str,str[i],i+1,size-1);

                    char temp=str[i];
                    str[i]=str[ceilIndex];
                    str[ceilIndex]=temp;

                    Arrays.sort(str,i+1,size);
                }
            }

        }

        public static void main(String[] args) {
            char[] str=new char[]{'A','C','B','C'};
            sortedPermutation(str);

        }
}
